package gson.emlog.activity;

import gson.emlog.R;
import gson.emlog.adapter.CloudBlogsAdapter;
import gson.emlog.utils.Action;
import gson.emlog.utils.Http;
import gson.emlog.utils.Result;

import org.json.simple.JSONArray;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class CloudBlogs extends Activity {
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.cloud_blogs);

		final ListView lv = (ListView) findViewById(R.id.cloud_blogs_view);

		lv.setOnItemClickListener(itemClick);
		Toast.makeText(context, "加载中ing", Toast.LENGTH_SHORT).show();
		Handler handler = new Handler();
		Runnable runnable = new Runnable() {
			public void run() {
				
				Http http = new Http(Action.CLOUD_BLOG);
				Result obj = http.post();
				JSONArray data = new JSONArray();
				if (obj.getStatus()) {
					data = (JSONArray) obj.getData();
				}else{
					Toast.makeText(context, obj.getData().toString(), Toast.LENGTH_SHORT).show();
				}
	
				CloudBlogsAdapter sadpt = new CloudBlogsAdapter(data, context);
				lv.setAdapter(sadpt);
			}
		};
		handler.postDelayed(runnable, 1 * 1000);
	}

	private AdapterView.OnItemClickListener itemClick = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> adptv, View v, int position,
				long id) {
			CloudBlogsAdapter adpt = (CloudBlogsAdapter) adptv.getAdapter();
			String url = adpt.getUrl(position);
			Uri uri = Uri.parse(url);
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(intent);
		}
	};
}
